//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package game;

import java.util.Random;

public class IANormal extends CPU {
	public IANormal(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	private Obolo[] myDeck;
	private Obolo[][] originalBoard;
	private Obolo[][] board;
	private IAOboloHelp possResult;
	private int modifyUpOrDown = 1;
	private Random rand = new Random();

	public IAOboloHelp bestOption(Game game) {
		possResult = new IAOboloHelp();
		originalBoard = game.getBoard();
		copyDeck(game.getPlayer(game.getTurn()).getDeck());
		copyBoard(game.getBoard());
		int parameter = 0;

		Rule[] rules = game.getRules();
		if (rules[0] instanceof RuleDownward) {
			modifyUpOrDown = -1;
		}
		// Index per agafar el Obolo real del deck del jugador
		int indexReal = 0;
		for (Obolo obolo : myDeck) {
			if (obolo != null) {
				for (int row = 0; row < board.length; row++)
					for (int col = 0; col < board[0].length; col++) {
						// poso tots els obolos possibles en totes les caselles
						// que
						// no estiguin ocupades

						if (board[row][col] == null) {
							board[row][col] = obolo;
							parameter = incrementTurn(game, row, col);
							// augmentem els parametres segons els llocs que
							// queden
							// buits
							if (row != board.length - 1)
								parameter += incrementAround(obolo.getSouth(),
										row + 1, col);
							else
								parameter += 10;

							if (row != 0)
								parameter += incrementAround(obolo.getNorth(),
										row - 1, col);
							else
								parameter += +10;

							if (col != board[0].length - 1)
								parameter += incrementAround(obolo.getEast(),
										row, col + 1);
							else
								parameter += 10;

							if (col != 0)
								parameter += incrementAround(obolo.getWest(),
										row, col - 1);
							else
								parameter += 10;
							board[row][col] = null;
							if (possResult.parameter < parameter
									|| (possResult.parameter == parameter && rand
											.nextInt(2) == 0)) {
								// if (possResult.parameter < parameter){
								possResult.obolo = game.getPlayerDeck(game
										.getTurn())[indexReal];
								possResult.parameter = parameter;
								possResult.column = col;
								possResult.row = row;
							}
						}
						game.setBoard(originalBoard);
					}
			}
			// Augmenta el index del deck d'on es treu el obolo
			indexReal++;
		}
		/*
		 * for (int i = 0 ; i<myDeck.length;i++){ if(myDeck[i]!=null &&
		 * myDeck[i] == possResult.obolo){ myDeck[i]=null; } }
		 */
		return possResult;
	}

	private void copyDeck(Obolo[] deck) {
		myDeck = new Obolo[deck.length];
		for (int i = 0; i < deck.length; i++) {
			try {
				if (deck[i] != null)
					myDeck[i] = deck[i].clone();
			} catch (CloneNotSupportedException e) {
				System.out.println("clone deck errorrrrrr!!!");
			}
		}
	}

	private void copyBoard(Obolo[][] boardGame) {
		originalBoard = boardGame;
		board = new Obolo[originalBoard.length][originalBoard[0].length];
		for (int i = 0; i < board.length; i++)
			for (int j = 0; j < board[0].length; j++) {
				try {
					if (originalBoard[i][j] != null) {
						board[i][j] = originalBoard[i][j].clone();
					}
				} catch (CloneNotSupportedException e) {
					System.out.println("clone board errorrrrrr!!!");
				}
			}
	}

	private int incrementAround(Skill skill, int row, int col) {
		int parameter = 0;
		if (originalBoard[row][col] == null) {
			if (skill instanceof SkillShield)
				parameter = 15;
			if (skill instanceof SkillNumber)
				parameter = ((SkillNumber) skill).getNumber() * modifyUpOrDown;
		} else
			parameter = 15;

		return parameter;
	}

	private int incrementTurn(Game game, int rowCheck, int colCheck) {
		int parameter = 0;

		game.setBoard(board);
		game.checkRules(rowCheck, colCheck);

		for (int row = 0; row < board.length; row++)
			for (int col = 0; col < board[0].length; col++)
				if (originalBoard[row][col] != null
						&& board[row][col].getOwner() != originalBoard[row][col]
								.getOwner()) {

					parameter += 50;
					board[row][col]
							.setOwner(originalBoard[row][col].getOwner());
				}

		return parameter;
	}
}
